package com.ccctop.crowd.util;

import com.ccctop.crowd.contract.CrowdFunding;
import org.web3j.protocol.Web3j;
import org.web3j.tx.TransactionManager;
import org.web3j.tx.gas.ContractGasProvider;
import org.web3j.tx.gas.StaticGasProvider;

import java.math.BigInteger;

public class Web3jUtil {
    private final static BigInteger GAS_LIMIT = BigInteger.valueOf(6721975L);
    private final static BigInteger GAS_PRICE = BigInteger.valueOf(20000000000L);
    private final static String UNIT_CONVERSION = "000000000000000000";

    // 部署合约
    public static String deployContract(Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) throws Exception {
        return CrowdFunding.deploy(web3j, transactionManager, contractGasProvider).send().getContractAddress();
    }

    // 获取合约实例
    public static CrowdFunding loadContract(String contractAddress, Web3j web3j, TransactionManager transactionManager) {
        return CrowdFunding.load(contractAddress, web3j, transactionManager, GAS_PRICE, GAS_LIMIT);
    }

    public static BigInteger unitConversion(double num){
        return new BigInteger(new Double(num).longValue()+UNIT_CONVERSION);
    }
    public static BigInteger unitConversion(Integer num){
        return new BigInteger(num+UNIT_CONVERSION);
    }

    public static ContractGasProvider getContractGasProvider(){
        return new StaticGasProvider(GAS_PRICE, GAS_LIMIT);
    }


}
